package com.oujiong.service;

import com.oujiong.Handler.OrderHandler;
import com.oujiong.Handler.OrderHandlerType;
import com.oujiong.entity.Order;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.annotation.AnnotationUtils;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.stream.Collectors;

/**
 * @author wuyonghao@myhexin.com
 * @description TODO
 * @date 2020/6/24 11:36
 */
@Service
public class OrderService {
    private Map<String, OrderHandler> orderHandlerMap;

    @Autowired
    public void setOrderHandlerMap(List<OrderHandler> orderHandlers) {
        // 注入各种类型的订单处理类
        orderHandlerMap = orderHandlers.stream().collect(Collectors.toMap(orderHandler -> Objects.requireNonNull(AnnotationUtils.findAnnotation(orderHandler.getClass(), OrderHandlerType.class)).source(), v -> v, (v1, v2) -> v1));
    }

    public void orderService(Order order) {
        OrderHandler orderHandler = orderHandlerMap.get(order.getSource());
        orderHandler.handle(order);
    }
}
